package org.apache.oozie.tools.workflowgenerator.client.property.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.oozie.tools.workflowgenerator.client.property.Property;
import org.apache.oozie.tools.workflowgenerator.client.property.PropertyTable;
import org.apache.oozie.tools.workflowgenerator.client.widget.NodeWidget;

import com.google.gwt.xml.client.Text;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;

public class MapReducePropertyTable extends PropertyTable {

    protected Grid grid;
    protected List<Property> configs;
    protected List<Property> prepare;
    protected List<Property> others;
    protected TextBox jt;
    protected TextBox nn;

    public MapReducePropertyTable(NodeWidget w) {
        super(w);
        init();
    }

    /**
     * Initialize a property table
     */
    protected void init() {
        initConf();
        initWidget();
    }

    /**
     * Initialize configuration
     */
    protected void initConf() {
        configs = new ArrayList<Property>();
        configs.add(new Property("mapred.mapper.class", ""));
        configs.add(new Property("mapred.reduce.class", ""));
        configs.add(new Property("mapred.job.queue.name", ""));

        prepare = new ArrayList<Property>();
        prepare.add(new Property("", ""));

        others = new ArrayList<Property>();
        others.add(new Property("", ""));

        widgetList = new ArrayList<NodeWidget>();
        updateWidgetList();
        widgetDropDown = new ArrayList<NodeWidget>();
        updateWidgetDropDown();
    }

    /**
     * Generate xml elements of mr action and attach them to xml doc
     */
    public void generateXML(Document doc, Element root, NodeWidget next) {

        Element action = doc.createElement("action");
        action.setAttribute("name", current.getName());

        // create <map-reduce>
        Element mrele = doc.createElement("map-reduce");
        action.appendChild(mrele);

        // create <job-tracker>
        Element jtele = doc.createElement("job-tracker");
        Text t = doc.createTextNode(jt.getText());
        jtele.appendChild(t);
        mrele.appendChild(jtele);

        // create <name-node>
        Element nnele = doc.createElement("name-node");
        Text n = doc.createTextNode(nn.getText());
        nnele.appendChild(n);
        mrele.appendChild(nnele);

        // create <prepare>
        prepareToXML(prepare, mrele, doc);

        // create <job-xml>
        filterListToXML(others, mrele, doc, "job-xml");

        // create <configuration>
        configToXML(configs, mrele, doc);

        // create <file>
        filterListToXML(others, mrele, doc, "file");

        // create <archive>
        filterListToXML(others, mrele, doc, "archive");

        Element okele = doc.createElement("ok");
        okele.setAttribute("to", next.getName());
        action.appendChild(okele);

        Element errele = doc.createElement("error");
        NodeWidget kill = getKillNode();
        errele.setAttribute("to", kill == null ? "" : kill.getName());
        action.appendChild(errele);

        root.appendChild(action);

    }

    /**
     * Initialize widgets shown in property table
     */
    protected void initWidget() {

        grid = new Grid(8, 2);
        this.add(grid);

        this.setAlwaysShowScrollBars(true);
        this.setSize("100%", "80%");

        // insert row for Name
        grid.setWidget(0, 0, createLabel("Name"));
        name = new TextBox();
        name.setText("MR"); // default value
        grid.setWidget(0, 1, formatCell(name));

        // insert row for OK
        grid.setWidget(1, 0, createLabel("OK"));
        okVal = new ListBox();
        createWidgetDropDownList(okVal);
        grid.setWidget(1, 1, formatCell(okVal));

        // insert row for ERROR
        grid.setWidget(2, 0, createLabel("Error"));
        errorVal = new ListBox();
        updateErrorDropDownList();
        grid.setWidget(2, 1, formatCell(errorVal));

        // insert row for JobTracker
        grid.setWidget(3, 0, createLabel("JobTracker"));
        jt = new TextBox();
        grid.setWidget(3, 1, formatCell(jt));

        // insert row for NameNode
        grid.setWidget(4, 0, createLabel("NameNode"));
        nn = new TextBox();
        grid.setWidget(4, 1, formatCell(nn));

        // insert row for prepare
        grid.setWidget(5, 0, createLabel("Prepare"));
        grid.setWidget(5, 1, createSubTable("Operation", "Path", prepare, Arrays.asList("", "delete", "mkdir")));

        // insert row for Configuration
        grid.setWidget(6, 0, createLabel("Configuration"));
        grid.setWidget(6, 1, createSubTable("Property Name", "Value", configs, null));

        // insert row for others
        grid.setWidget(7, 0, createLabel("Others"));
        grid.setWidget(7, 1, createSubTable("Tag", "value", others, Arrays.asList("", "job-xml", "file", "archive")));
    }
}
